<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像 -->
      <div class="avatar-box">
        <img src="../../assets/imgs/ruohaun.jpg" alt="">
      </div>
      <!-- 登陆表单 -->
      <el-form class="login-form" :model="loginForm" :rules="loginRules" ref="loginFormRef">
        <el-form-item prop="username" >
          <el-input  placeholder="请输入用户名" v-model="loginForm.username">
            <i slot="prefix" class="el-input__icon el-icon-s-custom"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="loginForm.password">
            <i slot="prefix" class="el-input__icon el-icon-question"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
    name:'login',
    data(){
        return{
            loginForm:{
            username:'admin',
            password:'123456',
            },// 表单验证
            loginRules:{
                username:[
                    // required 必须输入
                    // message 不满足条件的提示
                    // trigger
                    // min 最少 max 最多
                    {required: true, message: '请输入用户名', trigger: 'blur'},
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                password:[
                     {required: true, message: '请输入密码!', trigger: 'blur'},
                    { min: 3, max: 6, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    methods:{
        // 登录
        login(){
            this.$refs.loginFormRef.validate(async flag =>{
                if(!flag){
                    return this.$message.error('请输入正确的用户名和密码')
                }
               const {data} = await this.$http.post('/login',this.loginForm)
               console.log(data)
              //  登录失败
              if(data.meta.status!= 200){
                return this.$message.error('登录失败 请检查用户名密码')
              }
              // 登录成功
              this.$message.success(`登陆成功 欢迎您:${data.data.username}`)
              // 1.保存服务器响应的token
              // -a 因为后续所有请求都需要携带token
              // -b 全局守卫中需要通过token存在与否判断用户是否登录成功
              sessionStorage.setItem('token',data.data.token)
              // 2.跳转到首页
              this.$router.push('/home')
            })
        },
        // 重置
        reset(){
            // 通过表单获取对象
            this.$refs.loginFormRef.resetFields()
        },
    }
}
</script>

<style lang="scss" scoped>
.login-container {
  // background-color: slategray;
  background: url('../../assets/imgs/bg.jpg') no-repeat;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-size: cover;
  .login-box {
    width: 500px;
    height: 350px;
    background-color: #fff;
    position: relative;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 5px;
    box-shadow: 5px 5px 5px #ddd;
  }
  .avatar-box {
    width: 120px;
    height: 120px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 5px 5px 5px #ddd;
    img {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login-form {
    position: absolute;
    bottom: 40px;
    padding: 0 20px;
    width: 100%;
    box-sizing: border-box;
    .el-input__inner {
      height: 48px;
      font-size: 16px;
    }
    .el-input__icon {
      font-size: 16px;
      line-height: 41px;
    }
  }
  .btns {
    display: flex;
    justify-content: space-between;
  }
}
</style>